<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>


</head>

<body>
<div class="article-body">

    <div class="article-intro" id="content">

        <h1>Python3 <span class="color_h1">字典</span></h1>
        <p>字典是另一种可变容器模型，且可存储任意类型对象。</p>
        <p>字典的每个键值(key=&gt;value)对用冒号(<b>:</b>)分割，每个对之间用逗号(<b>,</b>)分割，整个字典包括在花括号(<b>{})</b>中 ,格式如下所示：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">d </span><span class="pun">=</span><span
                class="pln"> </span><span class="pun">{</span><span class="pln">key1 </span><span
                class="pun">:</span><span class="pln"> value1</span><span class="pun">,</span><span
                class="pln"> key2 </span><span class="pun">:</span><span class="pln"> value2 </span><span
                class="pun">}</span></pre>
        <p>键必须是唯一的，但值则不必。</p>
        <p>值可以取任何数据类型，但键必须是不可变的，如字符串，数字或元组。</p>
        <p>一个简单的字典实例：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">dict </span><span class="pun">=</span><span
                class="pln"> </span><span class="pun">{</span><span class="str">'Alice'</span><span class="pun">:</span><span
                class="pln"> </span><span class="str">'2341'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'Beth'</span><span class="pun">:</span><span
                class="pln"> </span><span class="str">'9102'</span><span class="pun">,</span><span
                class="pln"> </span><span class="str">'Cecil'</span><span class="pun">:</span><span class="pln"> </span><span
                class="str">'3258'</span><span class="pun">}</span></pre>
        <p>也可如此创建字典：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">dict1 </span><span class="pun">=</span><span
                class="pln"> </span><span class="pun">{</span><span class="pln"> </span><span
                class="str">'abc'</span><span class="pun">:</span><span class="pln"> </span><span class="lit">456</span><span
                class="pln"> </span><span class="pun">};</span><span class="pln">
dict2 </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span
                class="pln"> </span><span class="str">'abc'</span><span class="pun">:</span><span
                class="pln"> </span><span class="lit">123</span><span class="pun">,</span><span
                class="pln"> </span><span class="lit">98.6</span><span class="pun">:</span><span
                class="pln"> </span><span class="lit">37</span><span class="pln"> </span><span
                class="pun">};</span></pre>


        <h2>访问字典里的值</h2>
        <p>把相应的键放入熟悉的方括弧，如下实例:</p>

        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

dict </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span
                class="str">'Name'</span><span class="pun">:</span><span class="pln"> </span><span
                class="str">'Runoob'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'Age'</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">7</span><span class="pun">,</span><span class="pln"> </span><span class="str">'Class'</span><span
                class="pun">:</span><span class="pln"> </span><span class="str">'First'</span><span class="pun">}</span><span
                class="pln">

</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"dict['Name']: "</span><span
                class="pun">,</span><span class="pln"> dict</span><span class="pun">[</span><span
                class="str">'Name'</span><span class="pun">])</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"dict['Age']: "</span><span
                class="pun">,</span><span class="pln"> dict</span><span class="pun">[</span><span
                class="str">'Age'</span><span class="pun">])</span></pre>
        <p>以上实例输出结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">dict</span><span class="pun">[</span><span
                class="str">'Name'</span><span class="pun">]:</span><span class="pln">  </span><span
                class="typ">Runoob</span><span class="pln">
dict</span><span class="pun">[</span><span class="str">'Age'</span><span class="pun">]:</span><span
                class="pln">  </span><span class="lit">7</span></pre>
        <p>如果用字典里没有的键访问数据，会输出错误如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

dict </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span
                class="str">'Name'</span><span class="pun">:</span><span class="pln"> </span><span
                class="str">'Runoob'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'Age'</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">7</span><span class="pun">,</span><span class="pln"> </span><span class="str">'Class'</span><span
                class="pun">:</span><span class="pln"> </span><span class="str">'First'</span><span
                class="pun">};</span><span class="pln">

</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"dict['Alice']: "</span><span
                class="pun">,</span><span class="pln"> dict</span><span class="pun">[</span><span
                class="str">'Alice'</span><span class="pun">])</span></pre>
        <p>以上实例输出结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Traceback</span><span
                class="pln"> </span><span class="pun">(</span><span class="pln">most recent call </span><span
                class="kwd">last</span><span class="pun">):</span><span class="pln">
  </span><span class="typ">File</span><span class="pln"> </span><span class="str">"test.py"</span><span
                class="pun">,</span><span class="pln"> line </span><span class="lit">5</span><span
                class="pun">,</span><span class="pln"> </span><span class="kwd">in</span><span class="pln"> </span><span
                class="str">&lt;module&gt;</span><span class="pln">
    </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"dict['Alice']: "</span><span
                class="pun">,</span><span class="pln"> dict</span><span class="pun">[</span><span
                class="str">'Alice'</span><span class="pun">])</span><span class="pln">
</span><span class="typ">KeyError</span><span class="pun">:</span><span class="pln"> </span><span
                class="str">'Alice'</span></pre>
        <br>

        <h2>修改字典</h2>
        <p>向字典添加新内容的方法是增加新的键/值对，修改或删除已有键/值对如下实例:</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

dict </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span
                class="str">'Name'</span><span class="pun">:</span><span class="pln"> </span><span
                class="str">'Runoob'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'Age'</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">7</span><span class="pun">,</span><span class="pln"> </span><span class="str">'Class'</span><span
                class="pun">:</span><span class="pln"> </span><span class="str">'First'</span><span class="pun">}</span><span
                class="pln">

dict</span><span class="pun">[</span><span class="str">'Age'</span><span class="pun">]</span><span
                class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="lit">8</span><span
                class="pun">;</span><span class="pln">               </span><span class="com"># 更新 Age</span><span
                class="pln">
dict</span><span class="pun">[</span><span class="str">'School'</span><span class="pun">]</span><span
                class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span
                class="str">"菜鸟教程"</span><span class="pln">  </span><span class="com"># 添加信息</span><span class="pln">


</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"dict['Age']: "</span><span
                class="pun">,</span><span class="pln"> dict</span><span class="pun">[</span><span
                class="str">'Age'</span><span class="pun">])</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"dict['School']: "</span><span
                class="pun">,</span><span class="pln"> dict</span><span class="pun">[</span><span
                class="str">'School'</span><span class="pun">])</span></pre>
        以上实例输出结果：
        <pre class="prettyprint prettyprinted" style=""><span class="pln">dict</span><span class="pun">[</span><span
                class="str">'Age'</span><span class="pun">]:</span><span class="pln">  </span><span class="lit">8</span><span
                class="pln">
dict</span><span class="pun">[</span><span class="str">'School'</span><span class="pun">]:</span><span
                class="pln">  </span><span class="pun">菜鸟教程</span></pre>

        <br>

        <h2>删除字典元素</h2>

        <p>能删单一的元素也能清空字典，清空只需一项操作。</p>

        <p>显示删除一个字典用del命令，如下实例：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

dict </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span
                class="str">'Name'</span><span class="pun">:</span><span class="pln"> </span><span
                class="str">'Runoob'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'Age'</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">7</span><span class="pun">,</span><span class="pln"> </span><span class="str">'Class'</span><span
                class="pun">:</span><span class="pln"> </span><span class="str">'First'</span><span class="pun">}</span><span
                class="pln">

</span><span class="kwd">del</span><span class="pln"> dict</span><span class="pun">[</span><span
                class="str">'Name'</span><span class="pun">]</span><span class="pln"> </span><span class="com"># 删除键 'Name'</span><span
                class="pln">
dict</span><span class="pun">.</span><span class="pln">clear</span><span class="pun">()</span><span
                class="pln">     </span><span class="com"># 清空字典</span><span class="pln">
</span><span class="kwd">del</span><span class="pln"> dict         </span><span class="com"># 删除字典</span><span
                class="pln">

</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"dict['Age']: "</span><span
                class="pun">,</span><span class="pln"> dict</span><span class="pun">[</span><span
                class="str">'Age'</span><span class="pun">])</span><span class="pln">
</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"dict['School']: "</span><span
                class="pun">,</span><span class="pln"> dict</span><span class="pun">[</span><span
                class="str">'School'</span><span class="pun">])</span></pre>
        <p>但这会引发一个异常，因为用执行 del 操作后字典不再存在：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Traceback</span><span
                class="pln"> </span><span class="pun">(</span><span class="pln">most recent call </span><span
                class="kwd">last</span><span class="pun">):</span><span class="pln">
  </span><span class="typ">File</span><span class="pln"> </span><span class="str">"test.py"</span><span
                class="pun">,</span><span class="pln"> line </span><span class="lit">9</span><span
                class="pun">,</span><span class="pln"> </span><span class="kwd">in</span><span class="pln"> </span><span
                class="str">&lt;module&gt;</span><span class="pln">
    </span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"dict['Age']: "</span><span
                class="pun">,</span><span class="pln"> dict</span><span class="pun">[</span><span
                class="str">'Age'</span><span class="pun">])</span><span class="pln">
</span><span class="typ">TypeError</span><span class="pun">:</span><span class="pln"> </span><span
                class="str">'type'</span><span class="pln"> </span><span class="kwd">object</span><span
                class="pln"> </span><span class="kwd">is</span><span class="pln"> </span><span
                class="kwd">not</span><span class="pln"> subscriptable</span></pre>
        <p><strong>注：</strong>del() 方法后面也会讨论。</p>

        <br>

        <h3>字典键的特性</h3>

        <p>字典值可以是任何的 python 对象，既可以是标准的对象，也可以是用户定义的，但键不行。</p>

        <p>两个重要的点需要记住：</p>
        <p>

        </p>
        <p>1）不允许同一个键出现两次。创建时如果同一个键被赋值两次，后一个值会被记住，如下实例：</p>
        <p>
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

dict </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span
                class="str">'Name'</span><span class="pun">:</span><span class="pln"> </span><span
                class="str">'Runoob'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'Age'</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">7</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'Name'</span><span class="pun">:</span><span class="pln"> </span><span
                class="str">'小菜鸟'</span><span class="pun">}</span><span class="pln">

</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"dict['Name']: "</span><span
                class="pun">,</span><span class="pln"> dict</span><span class="pun">[</span><span
                class="str">'Name'</span><span class="pun">])</span></pre>
        <p>以上实例输出结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">dict</span><span class="pun">[</span><span
                class="str">'Name'</span><span class="pun">]:</span><span class="pln">  </span><span
                class="pun">小菜鸟</span></pre>

        <p>2）键必须不可变，所以可以用数字，字符串或元组充当，而用列表就不行，如下实例：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="com">#!/usr/bin/python3</span><span class="pln">

dict </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{[</span><span
                class="str">'Name'</span><span class="pun">]:</span><span class="pln"> </span><span
                class="str">'Runoob'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'Age'</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">7</span><span class="pun">}</span><span class="pln">

</span><span class="kwd">print</span><span class="pln"> </span><span class="pun">(</span><span class="str">"dict['Name']: "</span><span
                class="pun">,</span><span class="pln"> dict</span><span class="pun">[</span><span
                class="str">'Name'</span><span class="pun">])</span></pre>
        <p>以上实例输出结果：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">Traceback</span><span
                class="pln"> </span><span class="pun">(</span><span class="pln">most recent call </span><span
                class="kwd">last</span><span class="pun">):</span><span class="pln">
  </span><span class="typ">File</span><span class="pln"> </span><span class="str">"test.py"</span><span
                class="pun">,</span><span class="pln"> line </span><span class="lit">3</span><span
                class="pun">,</span><span class="pln"> </span><span class="kwd">in</span><span class="pln"> </span><span
                class="str">&lt;module&gt;</span><span class="pln">
    dict </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{[</span><span
                class="str">'Name'</span><span class="pun">]:</span><span class="pln"> </span><span
                class="str">'Runoob'</span><span class="pun">,</span><span class="pln"> </span><span
                class="str">'Age'</span><span class="pun">:</span><span class="pln"> </span><span
                class="lit">7</span><span class="pun">}</span><span class="pln">
</span><span class="typ">TypeError</span><span class="pun">:</span><span class="pln"> unhashable type</span><span
                class="pun">:</span><span class="pln"> </span><span class="str">'list'</span></pre>

        <br>

        <h2>字典内置函数&amp;方法</h2>
        <p>Python字典包含了以下内置函数：</p>
        <table class="reference">
            <tbody>
            <tr>
                <th width="5%">序号</th>
                <th width="25%">函数及描述</th>
                <th>实例</th>
            </tr>

            <tr>
                <td>1</td>
                <td>len(dict)<br>计算字典元素个数，即键的总数。</td>
                <td>
<pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
        class="pln"> dict </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span
        class="str">'Name'</span><span class="pun">:</span><span class="pln"> </span><span
        class="str">'Runoob'</span><span class="pun">,</span><span class="pln"> </span><span
        class="str">'Age'</span><span class="pun">:</span><span class="pln"> </span><span class="lit">7</span><span
        class="pun">,</span><span class="pln"> </span><span class="str">'Class'</span><span class="pun">:</span><span
        class="pln"> </span><span class="str">'First'</span><span class="pun">}</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> len</span><span class="pun">(</span><span class="pln">dict</span><span
        class="pun">)</span><span class="pln">
</span><span class="lit">3</span></pre>
                </td>
            </tr>
            <tr>
                <td>2</td>
                <td>str(dict)<br>输出字典，以可打印的字符串表示。</td>
                <td>
<pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
        class="pln"> dict </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span
        class="str">'Name'</span><span class="pun">:</span><span class="pln"> </span><span
        class="str">'Runoob'</span><span class="pun">,</span><span class="pln"> </span><span
        class="str">'Age'</span><span class="pun">:</span><span class="pln"> </span><span class="lit">7</span><span
        class="pun">,</span><span class="pln"> </span><span class="str">'Class'</span><span class="pun">:</span><span
        class="pln"> </span><span class="str">'First'</span><span class="pun">}</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> str</span><span class="pun">(</span><span class="pln">dict</span><span
        class="pun">)</span><span class="pln">
</span><span class="str">"{'Name': 'Runoob', 'Class': 'First', 'Age': 7}"</span></pre>
                </td>
            </tr>
            <tr>
                <td>3</td>
                <td>type(variable)<br>返回输入的变量类型，如果变量是字典就返回字典类型。</td>
                <td>
<pre class="prettyprint prettyprinted" style=""><span class="pun">&gt;&gt;&gt;</span><span
        class="pln"> dict </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span><span
        class="str">'Name'</span><span class="pun">:</span><span class="pln"> </span><span
        class="str">'Runoob'</span><span class="pun">,</span><span class="pln"> </span><span
        class="str">'Age'</span><span class="pun">:</span><span class="pln"> </span><span class="lit">7</span><span
        class="pun">,</span><span class="pln"> </span><span class="str">'Class'</span><span class="pun">:</span><span
        class="pln"> </span><span class="str">'First'</span><span class="pun">}</span><span class="pln">
</span><span class="pun">&gt;&gt;&gt;</span><span class="pln"> type</span><span class="pun">(</span><span class="pln">dict</span><span
        class="pun">)</span><span class="pln">
</span><span class="pun">&lt;</span><span class="kwd">class</span><span class="pln"> </span><span
        class="str">'dict'</span><span class="pun">&gt;</span></pre>
                </td>
            </tr>
            </tbody>
        </table>
        <p>Python字典包含了以下内置方法：</p>
        <table class="reference">
            <tbody>
            <tr>
                <th style="width:5%">序号</th>
                <th style="width:95%">函数及描述</th>
            </tr>
            <tr>
                <td>1</td>
                <td><a href="python3-att-dictionary-clear.html">radiansdict.clear()</a><br>删除字典内所有元素</td>
            </tr>
            <tr>
                <td>2</td>
                <td><a href="python3-att-dictionary-copy.html">radiansdict.copy()</a><br>返回一个字典的浅复制</td>
            </tr>
            <tr>
                <td>3</td>
                <td><a href="python3-att-dictionary-fromkeys.html">radiansdict.fromkeys()</a><br>
                    创建一个新字典，以序列seq中元素做字典的键，val为字典所有键对应的初始值
                </td>
            </tr>
            <tr>
                <td>4</td>
                <td><a href="python3-att-dictionary-get.html">radiansdict.get(key, default=None)</a><br>返回指定键的值，如果值不在字典中返回default值
                </td>
            </tr>
            <tr>
                <td>5</td>
                <td><a href="python3-att-dictionary-in.html">key in dict</a><br>如果键在字典dict里返回true，否则返回false</td>
            </tr>
            <tr>
                <td>6</td>
                <td><a href="python3-att-dictionary-items.html">radiansdict.items()</a><br>以列表返回可遍历的(键, 值) 元组数组</td>
            </tr>
            <tr>
                <td>7</td>
                <td><a href="python3-att-dictionary-keys.html">radiansdict.keys()</a><br>以列表返回一个字典所有的键</td>
            </tr>
            <tr>
                <td>8</td>
                <td><a href="python3-att-dictionary-setdefault.html">radiansdict.setdefault(key, default=None)</a><br>
                    和get()类似, 但如果键不存在于字典中，将会添加键并将值设为default
                </td>
            </tr>
            <tr>
                <td>9</td>
                <td><a href="python3-att-dictionary-update.html">radiansdict.update(dict2)</a><br>把字典dict2的键/值对更新到dict里
                </td>
            </tr>
            <tr>
                <td>10</td>
                <td><a href="python3-att-dictionary-values.html">radiansdict.values()</a><br>以列表返回字典中的所有值</td>
            </tr>
            <tr>
                <td>11</td>
                <td><a href="python3-att-dictionary-pop.html">pop(key[,default])</a><br>删除字典给定键 key
                    所对应的值，返回值为被删除的值。key值必须给出。
                    否则，返回default值。
                </td>
            </tr>
            <tr>
                <td>12</td>
                <td><a href="python3-att-dictionary-popitem.html"> popitem()</a><br>随机返回并删除字典中的一对键和值(一般删除末尾对)。</td>
            </tr>
            </tbody>
        </table>

    </div>
</div>
</body>
</html>
